﻿function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return "";
}
function toDBC(Str) {
    var DBCStr = "";
    for (var i = 0; i < Str.length; i++) {
        var c = Str.charCodeAt(i);
        if (c == 12288) {
            DBCStr += String.fromCharCode(32);
            continue;
        }
        if (c > 65280 && c < 65375) {
            DBCStr += String.fromCharCode(c - 65248);
            continue;
        }
        DBCStr += String.fromCharCode(c);
    }
    return DBCStr;
}

function FormatDate(temp) {
    var dateStr = padStr(temp.getFullYear()) + "-" +
                  padStr(1 + temp.getMonth()) + "-" +
                  padStr(temp.getDate());
    return dateStr;
}

function padStr(i) {
    return (i < 10) ? "0" + i : "" + i;
}


function GetPriceCurrentDay(prices, date) {
    for (var index = 0; index < prices.length; index++) {
        if (prices[index].date == FormatDate(date)) {
            return prices[index].price;
        }
    }
    return 0;
}
//
function WeekIndexToChinese(number) {
    switch (number) {
        case 0:
            return "日";
        case 1:
            return "一";
        case 2:
            return "二";
        case 3:
            return "三";
        case 4:
            return "四";
        case 5:
            return "五";
        case 6:
            return "六";
        default:
            return "";
    }
}

function ResetPrice(prices, price, sdate, edate, roomCount) {
    var result = 0;
    var startDate = str2date(toDBC(sdate));
    var endDate = str2date(toDBC(edate));
    for (var date = startDate; date < endDate; date = submitNextDay(date)) {
        var priceCurrentDay = GetPriceCurrentDay(prices, date); //获取当天价格
        if (priceCurrentDay > 0) {
            result += priceCurrentDay;
        }
        else {
            result += parseInt(price);
        }
    }
    return (isNaN(roomCount) || roomCount == "") ? 0 : result * parseInt(roomCount);
}
function SetHallPrice(price, num, day) {
    var result = 0;
    result = parseInt(price) * parseInt(num) * day;
    return result;
}
function str2date(str) {
    var d = null;
    var reg = /^(\d{4})-(\d{1,2})-(\d{1,2})$/
    if (arr = str.match(reg)) {
        d = new Date(Number(arr[1]), Number(arr[2]) - 1, Number(arr[3]));
    }
    return d;
}
function submitNextDay(d) {
    d.setDate(d.getDate() + 1);
    return d;
}
function NextDayStr(str) {
    var d = str2date(str);
    d.setDate(d.getDate() + 1);
    var mm = (d.getMonth() + 1);
    var dd = d.getDate();
    return d.getFullYear() + "-" + (mm < 10 ? "0" + mm : mm) + "-" + (dd < 10 ? "0" + dd : dd);
}
//截取字符串 包含中文处理 
//(串,长度,增加...) 
function subString(str, len, hasDot) {
    var newLength = 0;
    var newStr = "";
    var chineseRegex = /[^\x00-\xff]/g;
    var singleChar = "";
    var strLength = str.replace(chineseRegex, "**").length;
    for (var i = 0; i < strLength; i++) {
        singleChar = str.charAt(i).toString();
        if (singleChar.match(chineseRegex) != null) {
            newLength += 2;
        }
        else {
            newLength++;
        }
        if (newLength > len) {
            break;
        }
        newStr += singleChar;
    }
    if (hasDot && strLength > len) {
        newStr += "...";
    }
    return newStr;
}



function GetInPriceList(prices, price, sdate, edate) {
    var startDate = str2date(sdate);
    var endDate = str2date(edate);
    var html = "";
    for (var date = startDate; date < endDate; date = submitNextDay(date)) {
        html += "<li><p class=\"date\">" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
        var priceCurrentDay = GetPriceCurrentDay(prices, date); //获取当天价格
        if (priceCurrentDay > 0) {
            html += "(周" + WeekIndexToChinese(date.getDay()) + ")</p><p><span class=\"yahei\">￥</span><span class=\"price\">" + priceCurrentDay + "</span></p></li>";
        }
        else {
            html += "(周" + WeekIndexToChinese(date.getDay()) + ")</p><p><span class=\"yahei\">￥</span><span class=\"price\">" + price + "</span></p></li>";
        }

    }
    return html;
}
function GetContacts(uid) {
    $("#myContacts").html("");
    if (parseInt(uid) > 0) {
        $.getJSON("http://passport.lotour.com/order/MyContactListHandler.ashx?uid=" + uid + "&callback=?",
			function (data) {
			    var myhc = "";
			    $.each(data.contact, function (i) {
			        if (i < 8) {
			            var sname = subString(decodeURIComponent(data.contact[i].name), 10, false);
			            myhc += "<input id=\"chk_" + data.contact[i].id + "\" class=\"chkbox\" type=\"checkbox\" name=\"chk_contact\" onclick=\"FillInfo(this);\" value='{\"Name\":\"" + decodeURIComponent(data.contact[i].name) + "\",\"Mobile\":\"" + decodeURIComponent(data.contact[i].mobile) + "\",\"Email\":\"" + decodeURIComponent(data.contact[i].email) + "\"}' /><label>" + sname + "</label>";
			        }
			    });
			    $("#myContacts").html(myhc);
			});
    }
}
function GetLoginUser() {
    $.getJSON('http://my.lotour.com/user/external/userlogin.aspx?islogin=' + (new Date()) + '&callback=?',
        function (data) {
            if (parseInt(data.uid) > 0) {
                $("#hidUid").attr("value", data.uid);
            }
        });
}
function ShowLogin(obj) {
    var result;
    //smart.delegate = "doCurFavorite()";
    //smart.Options.showTarget = "aFavorite";
    checkIsLogin(obj);
}



function doCurFavorite() {
    $('div[id^=dlogin_]').hide();
    $('#dlogon_0').show();
    $('#aloginname').text(smart.user.nickname + ' 你好！');
alert(1);
    $.getJSON('http://my.lotour.com/user/external/userlogin.aspx?islogin=' + (new Date()) + '&callback=?',
        function (data) {
            if (parseInt(data.uid) > 0) {
                $("#hidUid").attr("value", data.uid);
                $("#BtnLogin").trigger("click");
            }
        });
    return false;
}


function CheckCustomNum(maxNum) {
    var patrn = /^\+?[1-9][0-9]{0,2}$/;
    if ($("#RoomCount").val() == "") {
        $("#CustomNumTip").html("请输入预订间数");
        $("#CustomNumTip").attr("class", "tip");
        $("#CustomNumTip").parent().attr("class", "orderroom bgc");
        return false;
    }
    if (!patrn.test(toDBC($("#RoomCount").val()))) {
        $("#CustomNumTip").html("请输入1-3位正整数");
        $("#CustomNumTip").attr("class", "tip");
        $("#CustomNumTip").parent().attr("class", "orderroom bgc");
        return false;
    }
    if (parseInt(toDBC($("#RoomCount").val())) > maxNum) {
        $("#CustomNumTip").html("您预订的间数不大于可预订总数，请重新输入");
        $("#CustomNumTip").attr("class", "tip");
        $("#CustomNumTip").parent().attr("class", "orderroom bgc");
        return false;
    }
    ClearCheckInfo("CountTip");
    return true;
}
function CheckInCount(maxNum) {
    var patrn = /^\+?[1-9][0-9]{0,2}$/;
    if ($("#Count").val() == "") {
        $("#CountTip").html("请输入入住人数");
        $("#CountTip").attr("class", "tip");
        $("#CountTip").parent().attr("class", "bgc");
        return false;
    }
    if (!patrn.test(toDBC($("#Count").val()))) {
        $("#CountTip").html("请输入1-3位正整数");
        $("#CountTip").attr("class", "tip");
        $("#CountTip").parent().attr("class", "bgc");
        return false;
    }
    if (parseInt(toDBC($("#Count").val())) > maxNum) {
        $("#CountTip").html("已超出会议室最大容纳人数，请您重新选择会议室或修改预订人数");
        $("#CountTip").attr("class", "tip");
        $("#CountTip").parent().attr("class", "bgc");
        return false;
    }
    ClearCheckInfo("CountTip");
    return true;
}
function CheckTimeSpan() {
    var patrn = /^\+?[1-9][0-9]{0,2}$/;
    if (!patrn.test(toDBC($("#TxtTimeSpan").val()))) {
        $("#TimeSpanTip").html("请输入1-3位正整数");
        $("#TimeSpanTip").attr("class", "tip");
        $("#TimeSpanTip").parent().attr("class", "orderroom bgc");
        return false;
    }
    ClearTimeSpan();
    return true;
}
function CheckUserName() {
    var patrn = /^([A-Za-z]|[\u4e00-\u9fa5])+$/;
    if ($.trim($("#UserName").val()) == "" || !patrn.test($.trim($("#UserName").val()))) {
        $("#UserNameTip").html("请填写入住人真实姓名");
        $("#UserNameTip").attr("class", "tip");
        $("#UserNameTip").parent().attr("class", "bgc");
        return false;
    }
    var namel = $.trim($("#UserName").val()).length;
    if (namel < 2 || namel > 20) {
        $("#UserNameTip").html("请填写2-20个字");
        $("#UserNameTip").attr("class", "tip");
        $("#UserNameTip").parent().attr("class", "bgc");
        return false;
    }
    ClearCheckInfo("UserNameTip");
    return true;
}
function CheckMobile() {
    var patrn = /^1[3458]\d{9}$/;
    if ($.trim($("#Mobile").val()) == "" || !patrn.test(toDBC($("#Mobile").val()))) {
        $("#MobileTip").html("请正确填写您的手机号码，以便及时确认预订信息");
        $("#MobileTip").attr("class", "tip");
        $("#MobileTip").parent().attr("class", "bgc");
        return false;
    }
    ClearCheckInfo("MobileTip");
    return true;
}
function CheckEmail() {
    var patrn = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    if ($.trim($("#Email").val()) != "" && !patrn.test(toDBC($("#Email").val()))) {
        $("#EmailTip").html("请正确填写您的邮箱地址，以便获得更多优惠信息");
        $("#EmailTip").attr("class", "tip");
        $("#EmailTip").parent().attr("class", "bgc");
        return false;
    }
    ClearCheckInfo("EmailTip");
    return true;
}
function CheckTel() {
    var patrn = /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}([-|转]\d{1,4})?$/;
    if ($.trim($("#TxtTel").val()) != "" && !patrn.test(toDBC($("#TxtTel").val()))) {
        $("#TelTip").html("请正确填写您的固定电话");
        $("#TelTip").attr("class", "tip");
        $("#TelTip").parent().attr("class", "bgc");
        return false;
    }
    ClearCheckInfo("TelTip");
    return true;
}
function CheckFax() {
    var patrn = /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}([-|转]\d{1,4})?$/;
    if ($.trim($("#TxtFax").val()) != "" && !patrn.test(toDBC($("#TxtFax").val()))) {
        $("#FaxTip").html("请正确填写您的传真号码");
        $("#FaxTip").attr("class", "tip");
        $("#FaxTip").parent().attr("class", "bgc");
        return false;
    }
    ClearCheckInfo("TelTip");
    return true;
}
function CheckAddress() {
    var patrn = /^([A-Za-z0-9-#_]|[\u4e00-\u9fa5])+$/;
    if ($.trim($("#TxtAddress").val()) != "" && !patrn.test(toDBC($("#TxtAddress").val()))) {
        $("#AddressTip").html("请正确填写您的联系地址");
        $("#AddressTip").attr("class", "tip");
        $("#AddressTip").parent().attr("class", "bgc");
        return false;
    }
    ClearCheckInfo("TelTip");
    return true;
}
function CheckRequire() {
    var text = $.trim($("#Require").val());
    var patrn = /^([A-Za-z0-9-#_,.，、。“” ]|[\u4e00-\u9fa5])+$/;
    if (text != "" && !patrn.test(text)) {
        $("#RequireTip").html("只可含“-,#,_”字符");
        $("#RequireTip").attr("class", "tip");
        $("#RequireTip").parent().attr("class", "bgc");
        return false;
    }
    if (text != "" && text.length > 200) {
        $("#RequireTip").html("补充说明不超过200字");
        $("#RequireTip").attr("class", "tip");
        $("#RequireTip").parent().attr("class", "bgc");
        return false;
    }
    ClearCheckInfo("RequireTip");
    return true;
}
function CheckReaded() {
    return $("#ChkReaded").attr("checked") == "checked";
}
function CheckPeice(entid, roomid) {
    $.getJSON("/hotel/03000000/api-GetVarPrice?r=" + Math.random() + "&entid=" + entid + "&roomId=" + roomid + "&selSdate=" + $("#sdate").val() + "&selEdate=" + $("#edate").val() + "&date=" + Math.random(), function (data) {
        if (parseInt(data) == -1) {
            alert(10);
        }
        return false;
    });
}
function CheckAll(maxNum) {
    return (CheckCustomNum(maxNum) & CheckInCount() & CheckUserName() & CheckMobile() & CheckEmail() & CheckRequire() & CheckReaded());
}
function CheckHallAll(maxCount) {
    //时长，参会人数，用户名，手机，电话，
    return (CheckTimeSpan() & CheckInCount(maxCount) & CheckUserName() & CheckMobile() & CheckTel() & CheckEmail() & CheckFax() & CheckAddress() & CheckRequire() & CheckReaded());
}
function ChangeRead() {
    if ($("#ChkReaded").attr("checked")) {
        $("#BtnSubmit").attr("class", "btn-confirm");
    }
    else {
        $("#BtnSubmit").attr("class", "btn-confirm btn-off");
    }
}
function ClearCheckInfo(id) {
    $("#" + id).attr("class", "");
    $("#" + id).html("");
    $("#" + id).parent().attr("class", "");
}
function ClearTimeSpan() {
    $("#TimeSpanTip").attr("class", "");
    $("#TimeSpanTip").html("");
    $("#TimeSpanTip").parent().attr("class", "orderroom");
}
function ireaded(url) {
    $("#ChkReaded").attr("checked", 'checked');
    $("#BtnSubmit").attr("class", "btn-confirm");
    window.open(url, "");
}
